package com.yjtc.cbg.commonutildemo.Util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;

/**
 * Created by chenboge on 2016/11/18.
 * 通过代码设置selector，一个设置点击和普通时的selector生成工具
 */
public class SelectorDrawableUtil {


    /*
    android.R.attr.onClick  点击效果
    android.R.attr.state_focused  获取焦点
     */
    public static StateListDrawable getSelector(Drawable normalDraw, Drawable pressedDraw) {
        StateListDrawable mListDrawable = new StateListDrawable();
        //手指按下时的效果,这里可以添加更多效果
        mListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDraw);
        //没有任何效果时显示的drawable
        mListDrawable.addState(new int[]{}, normalDraw);
        return mListDrawable;
    }

    public static StateListDrawable getSelector(Drawable[] drawables) {
        StateListDrawable mListDrawable = new StateListDrawable();
        //手指按下时的效果,这里可以添加更多效果
        mListDrawable.addState(new int[]{android.R.attr.state_pressed}, drawables[0]);
        //没有任何效果时显示的drawable
        mListDrawable.addState(new int[]{}, drawables[drawables.length - 1]);
        return mListDrawable;
    }

    /*
    前四个参数分别表示四个角的圆角弧度
    bgColor 背景颜色
    storkeWidth 边框宽度
    strokeColor 边框颜色
     */
    public static GradientDrawable getNormalDrawable(Context mContext, float topLeftCA, float topRigthCA, float buttomLeftCA, float buttomRightCA, int bgColor, int storkeWidth, int strokeColor) {
        GradientDrawable drawable = new GradientDrawable();
        //把边框值设置成dp对应的px
        storkeWidth = dp2px(mContext, storkeWidth);

        float[] circleAngleArr = {topLeftCA, topLeftCA, topRigthCA, topRigthCA,
                buttomLeftCA, buttomLeftCA, buttomRightCA, buttomRightCA};
        //把圆角设置成dp对应的px
        for (int i = 0; i < circleAngleArr.length; i++) {
            circleAngleArr[i] = dp2px(mContext, circleAngleArr[i]);
        }
        drawable.setCornerRadii(circleAngleArr);//圆角
        drawable.setColor(bgColor); //背景色
        drawable.setStroke(storkeWidth, strokeColor); //边框宽度，边框颜色
        return drawable;
    }


    /*
  前四个参数分别表示四个角的圆角弧度
  bgColor 背景颜色
  storkeWidth 边框宽度
  strokeColor 边框颜色
   */
    public static GradientDrawable getNormalDrawable(Context mContext, float radius, int bgColor, int storkeWidth, int strokeColor) {
        GradientDrawable drawable = new GradientDrawable();
        //把边框值设置成dp对应的px
        storkeWidth = dp2px(mContext, storkeWidth);

        //把圆角设置成dp对应的px
        radius = dp2px(mContext, radius);

        drawable.setCornerRadius(radius);//圆角
        drawable.setColor(bgColor); //背景色
        drawable.setStroke(storkeWidth, strokeColor); //边框宽度，边框颜色
        return drawable;
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

}
